package io.renren.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.R;
import io.renren.dao.SignUpPaymentLogDao;
import io.renren.entity.SchoolEvaluateEntity;
import io.renren.entity.SignUpPaymentLogEntity;
import io.renren.entity.StudentEntity;
import io.renren.util.StringUtils;
import io.renren.service.SchoolEvaluateService;
import io.renren.service.SignUpPaymentLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("signUpPaymentLogService")
public class SignUpPaymentLogServiceImpl extends ServiceImpl<SignUpPaymentLogDao, SignUpPaymentLogEntity> implements SignUpPaymentLogService {
    @Autowired
    private SchoolEvaluateService schoolEvaluateService;

    /***
     * 小程序
     * 评论科目
     * @param student 学员
     * @param teachStar 教学星
     * @param envStar 环境星
     * @param payStar 收费星
     * @param serverStar 服务星
     * @param contentDesc 文字
     * @param isName 是否匿名 0不用匿名 1要匿名
     * @param paymentLogId 订单id
     * @param imageUrl 图片
     * @return R
     */
    @Override
    public R evaluateSubject(StudentEntity student, float teachStar, float envStar, float payStar, float serverStar, String contentDesc, Integer isName, Integer paymentLogId,String imageUrl) {
        SignUpPaymentLogEntity signUpPaymentLog = baseMapper.selectById(paymentLogId);
        if (signUpPaymentLog == null) {
            return R.fail("订单不存在");
        }
        //查找评论是否存在
        SchoolEvaluateEntity schoolEvaluate = schoolEvaluateService.selectByPaymentLogId(paymentLogId);
        if (schoolEvaluate != null) {
            return R.fail("你已经评价了");
        }
        schoolEvaluate = new SchoolEvaluateEntity();
        schoolEvaluate.setContentDesc(contentDesc);
        schoolEvaluate.setCreateDate(new Date());
        if (envStar == 0.0f) {
            schoolEvaluate.setEnvStar(5f);
        } else {
            schoolEvaluate.setEnvStar(envStar);
        }
        if (teachStar == 0.0f) {
            schoolEvaluate.setTeachStar(5f);
        } else {
            schoolEvaluate.setTeachStar(teachStar);
        }
        if (teachStar == 0.0f) {
            schoolEvaluate.setServerStar(5f);
        } else {
            schoolEvaluate.setServerStar(serverStar);
        }
        if (teachStar == 0.0f) {
            schoolEvaluate.setPayStar(5f);
        } else {
            schoolEvaluate.setPayStar(payStar);
        }
        if(StringUtils.isNotBlank(imageUrl)){
            schoolEvaluate.setImageUrl(imageUrl);
        }
        schoolEvaluate.setSchoolId(student.getSchoolId());
        schoolEvaluate.setSubjectName(signUpPaymentLog.getSubjectName());
        schoolEvaluate.setPaymentLogId(paymentLogId);
        schoolEvaluate.setSubjectId(signUpPaymentLog.getSubjectId());
        if (isName == 1) {
            schoolEvaluate.setStudentName("匿名用户");
        } else {
            schoolEvaluate.setStudentName(student.getUsername());
        }
        schoolEvaluate.setStudentId(student.getUserId());
        schoolEvaluateService.insert(schoolEvaluate);


        return R.ok();
    }

    /***
     * 获取报名科目列表
     * @param student
     * @return R
     */
    @Override
    public R getSignUpSubjectList(StudentEntity student) {
        List<Map<String, Object>> signUpSubjectList = baseMapper.getSignUpSubjectList(student.getSchoolId());
        return R.ok(signUpSubjectList);
    }
}
